<?php

namespace app\api\controller\v1;

use \app\common\controller\ApiCommon;
use app\common\model\CourseAssistant;
use app\common\model\DutyArrange;
use app\common\model\Assistant;

//use \think\Db;

class DutyArranges extends ApiCommon
{

    protected $modelName = "DutyArrange";

    /**
     * 查询数据接口
     *
     * @return void
     */
    public function index()
    {
        $this->checkPermission(10);
        return parent::_index();
    }

    /**
     * 更新列表接口
     *
     * @return void
     */
    public function saveList()
    {
        $this->checkPermission(2);
        return parent::_saveList();
    }

    /**
     * 获取指定天在指定地点值班的助理
     *
     * @return void
     */
    public function getDutyAssistant()
    {
        //此接口不需要验证权限
        $request = $this->request;
        if (!($date = $request->param('date'))) {
            return  json_response([], STATUS_INVALD_PARAM, "必须传入日期参数");
        }
        $arrangeModel = new DutyArrange;
        if (false === ($ret = $arrangeModel->getDutyAssistantDetial($date, $request->param('building')))) {
            return json_response([], STATUS_FAIL, $arrangeModel->getError());
        } else {
            return json_response($ret);
        }

        // $param = $this->getTimeDetail($date);
        // if (!$param) {
        //     return json_response([], STATUS_FAIL, $param['errmsg']);
        // }
        // $arrangeQ = DutyArrange::where([
        //     'term_id' => $param['term']->id,
        //     'week' => $param['week']
        // ]);
        // if ($building = $request->param('building')) {
        //     $arrangeQ = $arrangeQ->where('building', $building);
        // }

        // $arrange = $arrangeQ->whereRaw('(duty_classweek&:class_week)<>0', ['class_week' => $param['class_week']])
        //     ->visible(['duty_classtime', 'assistant_id'])->select()->toArray();

        // $table = [];
        // foreach ($arrange as $v) {
        //     if (!isset($table[$v['assistant_id']])) {
        //         $table[$v['assistant_id']] = model("Assistant")->where('id', $v['assistant_id'])->visible(['name', 'class_name', 'phone'])->find()->toArray();
        //         $table[$v['assistant_id']]['duty_classtime'] = 0;
        //     }
        //     //考虑到值班表和助理表可能不同步可能导致找不到助理信息,故需要判断
        //     if ($table[$v['assistant_id']])
        //         $table[$v['assistant_id']]['duty_classtime'] |= $v['duty_classtime'];
        // }
        // usort($table, function ($a, $b) {
        //     return $a['duty_classtime'] - $b['duty_classtime'];
        // });

        // $ret = array();
        // foreach ($table as $k => $v) {
        //     $table[$k]['duty_classtime'] = bitmapToStr($table[$k]['duty_classtime']);
        //     array_push($ret, $table[$k]);
        // }

    }
}
